#include <func.h>
#include "head.h"

//先用一个结构体记录用户信息，不知道是否可行？
// typedef struct userinfo_s {
//     char user[20];
//     char salt[20];
//     char ciphertext[1024];
// } userinfo_t;

//如果数据库信息需要灵活，应该需要一个结构体，但先写死吧！

//数据库连接初始化
int databaseInit(MYSQL *db) {
    char *host = "localhost";
    char *user = "root";
    char *password = "root";
    char *database = "program2";
    MYSQL *ret = mysql_real_connect(db, host,user,password, database, 0, NULL, 0);
    if (ret != NULL) {
        //printf("db = %p, ret = %p\n", db, ret);
        //printf("Success!\n");
    }
    else {
        printf("ERROR: %s\n", mysql_error(db));
        exit(-1);
    }
}


//获取数据库用户密码表的信息
int getUserPasswd(userinfo_t *pInfo, char *userName) {

    MYSQL *db = mysql_init(NULL);
    databaseInit(db);

    char sql[300] = {0};
    strcpy(sql, "select salt from userPasswd where user = '");
    strcat(sql, userName);
    strcat(sql, "'");
    int qret = mysql_query(db, sql);
    SQL_ERROR_CHECK(db, qret, 0);
    MYSQL_RES *result = mysql_store_result(db);
    printf("The row number is %lld\n", mysql_num_rows(result));
    //有相同用户则找到一行结果，否则未找到！
    if (mysql_num_rows(result) == 1) {
        strcpy(pInfo->user, userName);
        //printf("用户已存在！");
        MYSQL_ROW row;
        int i = 0;
        while ((row = mysql_fetch_row(result))!= NULL) {
            for (int j = 0; j < mysql_num_fields(result); ++j) {
                strcpy(pInfo->salt, row[j]);
            }
        }
        strcpy(sql, "select ciphertext from userPasswd where user = '");
        strcat(sql, userName);
        strcat(sql, "'");
        int qret = mysql_query(db, sql);
        SQL_ERROR_CHECK(db, qret, 0);
        MYSQL_RES *result1 = mysql_store_result(db);
        //row;
        while ((row = mysql_fetch_row(result1))!= NULL) {
            for (int j = 0; j < mysql_num_fields(result1); ++j) {
                strcpy(pInfo->ciphertext, row[j]);
            }

        }
        mysql_free_result(result);
        mysql_free_result(result1);
        mysql_close(db);
        return 0;
    }
    else {
        mysql_free_result(result);
        mysql_close(db);
        return -1;
    }
}

//数据库增加用户
int userAdd(char *userName, char *passWord) {
    MYSQL *db = mysql_init(NULL);
    databaseInit(db);

    char salt[13] = {0};
    saltCreate(salt);
    //插入
    char sql[1024] = "insert into userPasswd values('";
    strcat(sql, userName);
    strcat(sql, "', '");
    strcat(sql, salt);
    strcat(sql, "', '");
    strcat(sql, crypt(passWord, salt));
    strcat(sql, "')");
    int qret = mysql_query(db, sql);
    SQL_ERROR_CHECK(db, qret, 0);
    //mysql_close(db);

    //用户注册成功的时候，创建用户家目录
    virDir_t *curVd = (virDir_t *)calloc(1, sizeof(virDir_t));
    //puts(dirName);
    int fd = open(userName, O_RDWR|O_CREAT|O_EXCL, 0666);
    ERROR_CHECK(fd, -1, "open");
    //MD5为主键，不允许重复，所以应该使其不为空
    close(fd);

    FILE *fp = fopen(userName, "a+");
    fwrite(userName, strlen(userName), 1, fp);
    //fwrite(preVd->user, sizeof(preVd->user), 1, fp);
    fwrite("'s file-warehouse", 17, 1, fp);
    fclose(fp);

    strcpy(curVd->name, userName);
    strcpy(curVd->pre_id, "0");
    char md5[1024] = {0};
    getMd5(userName, md5);
    strcpy(curVd->md5, md5);
    strcpy(curVd->user, userName);
    strcpy(curVd->isDelete, "N");
    strcpy(curVd->type, "d");

    bzero(sql, sizeof(sql));
    strcpy(sql, "insert into directory (name,pre_id,md5,user,isDelete,type) values('");
    sqlSplice(sql, curVd);
    free(curVd);
    qret = mysql_query(db, sql);
    SQL_ERROR_CHECK(db, qret, 0);
    mysql_close(db);
    return 0;
}

int saltCreate(char *salt) {
    char str[9] = {0};
    int i, flag;
    srand(time(NULL));
    for (int i = 0; i < 8; i++) {
        flag = rand()%3;
        switch(flag) {
        case 0:
            str[i] = rand()%26 + 'a';
            break;
        case 1:
            str[i] = rand()%26 + 'A';
        case 2:
            str[i] = rand()%10 + '0';
            break;
        }
    }
    strcpy(salt, "$6$");
    strcat(salt, str);
    strcat(salt, "$");
    return 0;
}